home *** CD-ROM | disk | FTP | other *** search
- Path: news.cs.ucla.edu!geoff
- From: geoff@ficus.cs.ucla.edu (Geoff Kuenning)
- Newsgroups: comp.lang.c++
- Subject: Derived classes inter-referencing each other
- Date: 30 Jan 1996 21:51:38 GMT
- Organization: Ficus Research Project, UCLA Computer Science Department
- Message-ID: <4em3ta$2p0@delphi.cs.ucla.edu>
- NNTP-Posting-Host: exeter.cs.ucla.edu
-
- My problem hasn't gotten quite this bad, so I was able to solve it
- by re-ordering declarations. But I started to wonder how one would
- handle the more complex case.
-
- Suppose you have a pair of derived classes, foo and bar, derived from
- foobase and barbase respectively. The bases can be declared like
- this:
-
- class foobase {
- virtual barbase& givebar();
- };
- class barbase {
- virtual foobase& givefoo();
- };
-
- So far, so good. Now we declare the derived classes. However, we'd
- like givebar() to return the corresponding derived class:
-
- class foo : public foobase {
- virtual bar& givebar();
- };
-
- Oops! Can't do that, because the compiler doesn't yet know that bar
- is derived from barbase. I tried putting a null declaration before foo:
-
- class bar : public barbase;
-
- but unsurprisingly the compiler wasn't too happy with this. The only
- solution I can come up with is to declare foo "incorrectly":
-
- class foo : public foobase {
- virtual barbase& givebar();
- };
- class bar : public barbase {
- virtual foo& givefoo(); // This is asymmetric but correct
- };
-
- and then use typecasts to get the result of givebar() to be of type bar.
-
- Anybody got a better solution?
- --
- Geoff Kuenning g.kuenning@ieee.org geoff@ITcorp.com
- http://www.cs.ucla.edu/ficus-members/geoff/
-